package cn.zld.sh.sh.util;
/**
 * @author xing
 * @time 2017-7-16 下午6:02:50
 */

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import cn.zld.sh.sh.base.BaseApplication;

/**
 * @author xing
 * @time 2017年8月8日09:24:48
 * @描述 保存登录工具类
 */
public class SharedUtils {
    public final static SharedPreferences sp = BaseApplication.getSystemConfigSharedFile();

    /** 保存登录bean对象的key值 */
    public static final String LOGIN_BEAN = "login_bean";

    /**
     * @描述 序列化对象
     * @return
     * @throws IOException
     */
    public static String serialize(Object object) throws IOException {
        long startTime = System.currentTimeMillis();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        long endTime = System.currentTimeMillis();
        return serStr;
    }

    /**
     * @描述 反序列化对象
     * @author xing
     * @time 2017年8月8日09:32:08
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static <T> T deSerialization(String str) throws IOException, ClassNotFoundException {
        // long startTime = System.currentTimeMillis();
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        @SuppressWarnings("unchecked")
        T t = (T) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        long endTime = System.currentTimeMillis();
        // LogUtils.e("serial", "反序列化耗时为:" + (endTime - startTime));
        return t;
    }
    /**
     * 创建清除sp数据的方法
     */
    public static void clearData(){
        if(sp != null){
            sp.edit().clear().apply();
        }
    }
    /**
     * @描述 保存对象--序列化对象并保存到SharedPreferences
     * @author xing
     * @param key
     *            ---对象名--LOGIN_BEAN
     * @param object
     * @return
     */
    public static boolean saveObject(String key, Object object) {
        Editor edit = sp.edit();
        String strObject = null;
        try {
            strObject = serialize(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(strObject)) {
            // 序列化对象失败
            return false;
        }
        edit.putString(key, strObject);
        edit.commit();
        return true;
    }

    /**
     * @描述 获取对象
     * @author xing
     * @param key
     *            --对象名--LOGIN_BEAN
     * @return
     */
    public static <T> T getObject(String key) {
        try {
            return deSerialization(sp.getString(key, ""));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * put string preferences
     *
     *            context
     * @param key
     *            The name of the preference to modify
     * @param value
     *            The new value for the preference
     * @return True if the new values were successfully written to persistent
     *         storage.
     */
    public static boolean putString(String key, String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * get string preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a string
     */
    public static String getString(String key) {
        return getString(key, null);
    }

    /**
     * get string preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @param defaultValue
     *            Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a string
     */
    public static String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * put int preferences
     *
     *            context
     * @param key
     *            The name of the preference to modify
     * @param value
     *            The new value for the preference
     * @return True if the new values were successfully written to persistent
     *         storage.
     */
    public static boolean putInt(String key, int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 清除某项Key  -- xing
     * @param key 标签
     * @return 是否删除
     */
    public static boolean RemoveValue(String key){
        Editor editor = sp.edit();
        editor.remove(key);
        return editor.commit();
    }

    /**
     * get int preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a int
     */
    public static int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * get int preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @param defaultValue
     *            Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a int
     */
    public static int getInt(String key, int defaultValue)	 {
        return sp.getInt(key, defaultValue);
    }

    /**
     * put long preferences
     *
     *            context
     * @param key
     *            The name of the preference to modify
     * @param value
     *            The new value for the preference
     * @return True if the new values were successfully written to persistent
     *         storage.
     */
    public static boolean putLong(String key, long value) {
        Editor editor = sp.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * get long preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a long
     */
    public static long getLong(String key) {
        return getLong(key, -1);
    }

    /**
     * get long preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @param defaultValue
     *            Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a long
     */
    public static long getLong(String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * put float preferences
     *
     *            context
     * @param key
     *            The name of the preference to modify
     * @param value
     *            The new value for the preference
     * @return True if the new values were successfully written to persistent
     *         storage.
     */
    public static boolean putFloat(String key, float value) {
        Editor editor = sp.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    /**
     * get float preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a float
     */
    public static float getFloat(String key) {
        return getFloat(key, -1);
    }

    /**
     * get float preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @param defaultValue
     *            Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a float
     */
    public static float getFloat(String key, float defaultValue) {
        return sp.getFloat(key, defaultValue);
    }

    /**
     * put boolean preferences
     *
     *            context
     * @param key
     *            The name of the preference to modify
     * @param value
     *            The new value for the preference
     * @return True if the new values were successfully written to persistent
     *         storage.
     */
    public static boolean putBoolean(String key, boolean value) {

        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * get boolean preferences, default is false
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a boolean
     */
    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * get boolean preferences
     *
     *            context
     * @param key
     *            The name of the preference to retrieve
     * @param defaultValue
     *            Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     *         ClassCastException if there is a preference with this name that
     *         is not a boolean
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        Log.i("spsp", "spsp");
        Log.i("spsp", sp.toString());
        return sp.getBoolean(key, defaultValue);
    }
}
